<?php
/**********************
**addstrip.php
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

require_once("private/top.inc");
require_once("private/login.inc");
require_once("private/arrays.inc");
require_once("private/intellistrip.inc");
require_once("private/room.inc");


function addStripCheck()
{
	//Check if a strip has been submitted
	if( !isset($_POST['stripRoom']) || !isset($_POST['stripName']) )
	{
		return TT_OK;
	}
	
	//Check if optional parameters have been specified, if not they will not be added and Intellistrip will use the default values
	$room = $_POST['stripRoom'];
	$name = $_POST['stripName'];
	if(isset($_POST['stripHostName']))
	{
		$hostName = $_POST['stripHostName'];
	}
	else
	{
		$hostName = null;
	}
	if(isset($_POST['stripServerPath']))
	{
		$serverPath = $_POST['stripServerPath'];
	}
	else
	{
		$serverPath = null;
	}
	if(isset($_POST['stripPort']))
	{
		$port = $_POST['stripPort'];
	}
	else
	{
		$port = null;
	}
	
	if( $room == '' || $name == '' )
	{
		echo "Room and Name are required fields.";
	}
	else
	{
		//Construct parameter array to feed createNewIntellistrip
		$arr = array('roomID' => $room, 'name' => $name);
		if($hostName)
		{
			$arr['hostName']= $hostName;
		}
		if($serverPath)
		{
			$arr['serverPath']= $serverPath;
		}
		if($port)
		{
			$arr['port']= $port;
		}
		
		//create Intellistrip and check that creation succeeded
		$rc = Intellistrip::createNewIntellistrip($stripID, $arr);
		if( TT_FAILED($rc) )
		{
			echo "There was a problem adding the Intellistrip to the system: " . Intellistrip::getLastError();
		}
		else
		{
			echo "Intellistrip was added successfully!";
		}
	}
	echo "
	<BR>
	<BR>
	";

}

function addStripForm()
{
	//As the software automatically determines the rest the only values the user need be concerned with are the room, serial, MAC, and name
	$formStr = "";
	
	$formStr .= "Please fill out this form.<br />\n";
	
	$formStr .= "<form name=\"AddStripForm\" method=\"Post\" action=\"addstrip.php\">\n";
	
	$formStr .= "<h3>What room is it in?</h3><select name=\"stripRoom\" size=\"1\">\n";
  	
	$formStr .= populateRooms();
	
	$formStr .= "</h3>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"editrooms.php\">edit rooms</a><br /><br/>\n";
	
	$formStr .= "<h3>Intellistrip Name: <input name=\"stripName\" type=\"text\" size=\"20\" maxlength=\"24\" /></h3><br />";
	$formStr .= "<h3>IP address: <input name=\"stripHostName\" value=\"127.0.0.1\" type=\"text\" size=\"20\" maxlength=\"255\" /></h3><br />";
	$formStr .= "<h3>Server Path: <input name=\"stripServerPath\" value=\"software_intellistrip/xml_rpc_server.php\" type=\"text\" size=\"20\" maxlength=\"255\" /></h3><br />";
	$formStr .= "<h3>Network Port: <input name=\"stripPort\" value=\"80\" type=\"text\" size=\"20\" maxlength=\"24\" /></h3><br />";

	$formStr .= "
				</select>
				<br />
				";
				
	$formStr .= "
				Before submiting, make sure Intellistrip<br />
				is on and plugged into an ethernet port.<br />
				";
				
	$formStr .= "
				<br /><input name=\"Submit\" type=\"submit\" value=\"Submit\"/>
	 			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"main.php\">Cancel</a>
				</h3>
				</form><br />
				";
	$formStr .= "
				<SCRIPT type=\"text/javascript\" language=\"JavaScript\">
				document.forms['AddStripForm'].elements['stripRoom'].focus();
				</SCRIPT>
				";
				
	echo $formStr;
	
}

//Creates the options list for the Rooms dropdown
function populateRooms()
{
	$strBuf = "";
	$rooms = null;
	$rc = Room::getRooms($rooms, true);
	if( TT_FAILED($rc) )
	{
		echo "There was a problem getting the list of rooms: " . Room::getLastError();
		return "";
	}
	if(!$rooms)
	{
		return "";
	}
	$rm = null;
	foreach( $rooms as $key => $value )
	{
		$strBuf .= "<OPTION value=\"". $value['roomID'] . "\">" . $value['name'] . "</OPTION>";
	}
	$strBuf .= "</select>";
	return $strBuf;
}

function displayPage()
{
	top("Add Intellistrip");
	addStripCheck();
	addStripForm();
	
	
	bottom();
}

displayPage();
?>